/*
 * TelaVeiculoCadastro.java
 *
 * Created on __DATE__, __TIME__
 */

package telas.aluguel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.swing.JCheckBox;
import business.aluguel.Acessorio;
import business.aluguel.AcessoriosVeiculos;
import business.aluguel.Modelo;
import business.aluguel.Veiculo;
import business.sistema.ExceptionSistema;
import business.sistema.IObjetoSistema;
import business.util.Menssagens;
import telas.comum.TelaDialogoCadastro;

/**
 *
 * @author  __USER__
 */
public class TelaVeiculoCadastro extends TelaDialogoCadastro
{
	private static final long serialVersionUID = 3020769462131815619L;

	/** Creates new form TelaVeiculoCadastro */
	public TelaVeiculoCadastro(java.awt.Frame parent, boolean modal)
			throws Exception
	{
		super(parent, modal);
		initComponents();
	}

	public TelaVeiculoCadastro(IObjetoSistema objetoSistema, boolean novo)
			throws Exception
	{
		super(objetoSistema, novo);
		initComponents();
		adicionaCheckBoxsAcessorios();

		if (!novo)
		{
			setTitle("Alterar Veiculo");
			preencherFormulario();
		} else
		{
			setTitle("Cadastrar Veiculo");
		}

		eventoSalvar();
		eventoFechar();

		setVisible(true);
	}

	@Override
	public void preencherFormulario() throws Exception
	{
		Veiculo veiculo = (Veiculo) getObjDados();

		cplModelo.setObjeto(veiculo.getModelo());
		jftPlaca.setText(veiculo.getPlaca());
		ctAnoFabricacao.setText(veiculo.getAnoFabricacao());
		jcbSegurado.setSelected(veiculo.isSegurado());
		cndQuilometragem.setText(String.valueOf(veiculo.getQuilometragem()));
		cndValorAluguel.setText(String.valueOf(veiculo.getValorAluguel()));
		cndValorVenda.setText(String.valueOf(veiculo.getValorVenda()));

		selecionaAcessorios(veiculo.getAcessoriosVeiculo());
	}

	private void selecionaAcessorios(Set<AcessoriosVeiculos> acessorios)
	{
		for (AcessoriosVeiculos acessorio : acessorios)
		{
			for (JCheckBox jCheckBox : listaCheckBoxs)
			{
				if (jCheckBox.getText().equals(acessorio.getNomeAcessorio()))
				{
					jCheckBox.setSelected(true);
					break;
				}
			}
		}
	}

	private void eventoSalvar()
	{
		botBotoes.getBtSalvar().addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					salvar();
					limpaCadastro();

				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	@Override
	public void preencheObjeto() throws Exception
	{
		String menssagem = "";

		Modelo modelo = (Modelo) cplModelo.getObjeto();
		if (modelo.getCodigo() == 0)
		{
			menssagem += "Modelo " + Menssagens.itemObrigatorio;
		}

		if (jftPlaca.getText().equals("   -    "))
		{
			menssagem += "Placa " + Menssagens.itemObrigatorio;
		}

		if (ctAnoFabricacao.getText().equals(""))
		{
			menssagem += "Ano Fabricação " + Menssagens.itemObrigatorio;
		}

		if ((cndQuilometragem.getText().equals(""))
				|| cndQuilometragem.getText().equals("0,00"))
		{
			menssagem += "Quilometragem " + Menssagens.itemObrigatorio;
		}

		if ((cndValorAluguel.getText().equals(""))
				|| cndValorAluguel.getText().equals("0,00"))
		{
			menssagem += "Valor Aluguel " + Menssagens.itemObrigatorio;
		}

		if ((cndValorVenda.getText().equals(""))
				|| cndValorVenda.getText().equals("0,00"))
		{
			menssagem += "Valor Venda " + Menssagens.itemObrigatorio;
		}

		if (menssagem.equals(""))
		{
			Veiculo veiculo = (Veiculo) getObjDados();

			veiculo.setModelo(modelo);
			veiculo.setPlaca(jftPlaca.getText());
			veiculo.setAnoFabricacao(ctAnoFabricacao.getText());
			veiculo.setSegurado(jcbSegurado.isSelected());
			veiculo.setQuilometragem(Double.parseDouble(cndQuilometragem
					.getNumero()));
			veiculo.setValorAluguel(Double.parseDouble(cndValorAluguel
					.getNumero()));
			veiculo
					.setValorVenda(Double
							.parseDouble(cndValorVenda.getNumero()));

			verificaAcessorios(veiculo);
		} else
		{
			throw new Exception(menssagem);
		}
	}

	private void verificaAcessorios(Veiculo veiculo)
	{
		for (JCheckBox jCheckBox : listaCheckBoxs)
		{
			if (jCheckBox.isSelected())
			{
				for (IObjetoSistema objeto : acessorios)
				{
					Acessorio acessorio = (Acessorio) objeto;

					if (jCheckBox.getText().equals(acessorio.getNome()))
					{
						veiculo.addAcessorio(acessorio);
					}
				}
			} else
			{
				for (IObjetoSistema objeto : acessorios)
				{
					Acessorio acessorio = (Acessorio) objeto;

					if (jCheckBox.getText().equals(acessorio.getNome()))
					{
						veiculo.removeAcessorio(acessorio);
					}
				}
			}
		}
	}

	@Override
	public void limpaCadastro() throws Exception
	{
		cplModelo.limpar();
		jftPlaca.setText("");
		ctAnoFabricacao.setText("");
		jcbSegurado.setSelected(false);
		cndQuilometragem.setText("");
		cndValorAluguel.setText("");
		cndValorVenda.setText("");

		for (JCheckBox jCheckBox : listaCheckBoxs)
		{
			jCheckBox.setSelected(false);
		}
	}

	private void eventoFechar()
	{
		botBotoes.getBtFechar().addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent e)
			{
				try
				{
					fecharTela();
				} catch (Exception ex)
				{
					new ExceptionSistema(ex);
				}
			}
		});
	}

	private ArrayList<String[]> formatarGridPesquisaModelo()
	{
		ArrayList<String[]> descricaoColunas = new ArrayList<String[]>();
		descricaoColunas.add(new String[]
		{ "Modelo", "getNome" });

		return descricaoColunas;
	}

	private void adicionaCheckBoxsAcessorios()
	{
		acessorios = new Acessorio().obterTodos();

		for (IObjetoSistema objeto : acessorios)
		{
			Acessorio acessorio = (Acessorio) objeto;

			JCheckBox checkBox = new JCheckBox(acessorio.getNome());

			pnlAcessorios.add(checkBox);

			listaCheckBoxs.add(checkBox);
		}
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents()
	{

		pnlPrincipal = new telas.componentes.Painel();
		botBotoes = new telas.componentes.BarraOperacoesTela();
		pnlCadastro = new telas.componentes.Painel();
		rotulo1 = new telas.componentes.Rotulo();
		cplModelo = new telas.componentes.CampoPopupLocalizador();
		rotulo2 = new telas.componentes.Rotulo();
		jftPlaca = new javax.swing.JFormattedTextField();
		rotulo3 = new telas.componentes.Rotulo();
		ctAnoFabricacao = new telas.componentes.CampoTexto();
		jcbSegurado = new javax.swing.JCheckBox();
		rotulo4 = new telas.componentes.Rotulo();
		cndQuilometragem = new telas.componentes.CampoNumeroDecimal();
		rotulo5 = new telas.componentes.Rotulo();
		rotulo6 = new telas.componentes.Rotulo();
		cndValorAluguel = new telas.componentes.CampoNumeroDecimal();
		cndValorVenda = new telas.componentes.CampoNumeroDecimal();
		rotulo7 = new telas.componentes.Rotulo();
		pnlAcessorios = new telas.componentes.Painel();

		setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

		pnlPrincipal.setMinimumSize(new java.awt.Dimension(151, 20));
		pnlPrincipal.setLayout(new java.awt.BorderLayout());
		pnlPrincipal.add(botBotoes, java.awt.BorderLayout.PAGE_END);

		pnlCadastro.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
		pnlCadastro.setMinimumSize(new java.awt.Dimension(100, 50));

		rotulo1.setText("Modelo:");

		cplModelo.setAtributoVisualizacao("Nome");
		cplModelo.setCampoPesquisa("nome");
		cplModelo.setClassePesquisa("business.aluguel.Modelo");
		cplModelo.setDescricaoPesquisa("Nome:");
		cplModelo.setFormatoGridPesquisa(formatarGridPesquisaModelo());

		rotulo2.setText("Placa:");

		try
		{
			jftPlaca
					.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(
							new javax.swing.text.MaskFormatter("AAA-####")));
		} catch (java.text.ParseException ex)
		{
			ex.printStackTrace();
		}

		rotulo3.setText("Ano Fabrica\u00e7\u00e3o:");

		ctAnoFabricacao.setMaxLength(4);

		jcbSegurado.setText("Segurado");

		rotulo4.setText("Quilometragem:");

		rotulo5.setText("Aluguel:");

		rotulo6.setText("Venda:");

		rotulo7.setText("Acess\u00f3rios:");

		pnlAcessorios.setBorder(javax.swing.BorderFactory
				.createEtchedBorder(javax.swing.border.EtchedBorder.RAISED));
		pnlAcessorios.setLayout(new java.awt.GridLayout(0, 2));

		javax.swing.GroupLayout pnlCadastroLayout = new javax.swing.GroupLayout(
				pnlCadastro);
		pnlCadastro.setLayout(pnlCadastroLayout);
		pnlCadastroLayout
				.setHorizontalGroup(pnlCadastroLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnlCadastroLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																rotulo1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																rotulo2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																rotulo4,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																rotulo5,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																rotulo6,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																rotulo7,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																cplModelo,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																483,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addGroup(
																pnlCadastroLayout
																		.createSequentialGroup()
																		.addGroup(
																				pnlCadastroLayout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING,
																								false)
																						.addComponent(
																								pnlAcessorios,
																								javax.swing.GroupLayout.Alignment.LEADING,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								javax.swing.GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)
																						.addGroup(
																								javax.swing.GroupLayout.Alignment.LEADING,
																								pnlCadastroLayout
																										.createSequentialGroup()
																										.addGroup(
																												pnlCadastroLayout
																														.createParallelGroup(
																																javax.swing.GroupLayout.Alignment.TRAILING,
																																false)
																														.addComponent(
																																cndValorVenda,
																																javax.swing.GroupLayout.Alignment.LEADING,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																Short.MAX_VALUE)
																														.addComponent(
																																cndValorAluguel,
																																javax.swing.GroupLayout.Alignment.LEADING,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																Short.MAX_VALUE)
																														.addComponent(
																																jftPlaca)
																														.addComponent(
																																cndQuilometragem,
																																javax.swing.GroupLayout.DEFAULT_SIZE,
																																122,
																																Short.MAX_VALUE))
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
																										.addComponent(
																												rotulo3,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												javax.swing.GroupLayout.DEFAULT_SIZE,
																												javax.swing.GroupLayout.PREFERRED_SIZE)
																										.addPreferredGap(
																												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
																										.addComponent(
																												ctAnoFabricacao,
																												javax.swing.GroupLayout.PREFERRED_SIZE,
																												53,
																												javax.swing.GroupLayout.PREFERRED_SIZE)))
																		.addGap(
																				18,
																				18,
																				18)
																		.addComponent(
																				jcbSegurado)))
										.addContainerGap()));
		pnlCadastroLayout
				.setVerticalGroup(pnlCadastroLayout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								pnlCadastroLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.CENTER)
														.addComponent(
																rotulo1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cplModelo,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo2,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jftPlaca,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																21,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																rotulo3,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																ctAnoFabricacao,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																jcbSegurado))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo4,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cndQuilometragem,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo5,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cndValorAluguel,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																rotulo6,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cndValorVenda,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
										.addGroup(
												pnlCadastroLayout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																rotulo7,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																pnlAcessorios,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																16,
																Short.MAX_VALUE))
										.addGap(139, 139, 139)));

		pnlPrincipal.add(pnlCadastro, java.awt.BorderLayout.CENTER);

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.DEFAULT_SIZE,
				javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
		layout.setVerticalGroup(layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addComponent(
				pnlPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, 379,
				javax.swing.GroupLayout.PREFERRED_SIZE));

		pack();
	}// </editor-fold>
	//GEN-END:initComponents

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private telas.componentes.BarraOperacoesTela botBotoes;
	private telas.componentes.CampoNumeroDecimal cndQuilometragem;
	private telas.componentes.CampoNumeroDecimal cndValorAluguel;
	private telas.componentes.CampoNumeroDecimal cndValorVenda;
	private telas.componentes.CampoPopupLocalizador cplModelo;
	private telas.componentes.CampoTexto ctAnoFabricacao;
	private javax.swing.JCheckBox jcbSegurado;
	private javax.swing.JFormattedTextField jftPlaca;
	private telas.componentes.Painel pnlAcessorios;
	private telas.componentes.Painel pnlCadastro;
	private telas.componentes.Painel pnlPrincipal;
	private telas.componentes.Rotulo rotulo1;
	private telas.componentes.Rotulo rotulo2;
	private telas.componentes.Rotulo rotulo3;
	private telas.componentes.Rotulo rotulo4;
	private telas.componentes.Rotulo rotulo5;
	private telas.componentes.Rotulo rotulo6;
	private telas.componentes.Rotulo rotulo7;
	// End of variables declaration//GEN-END:variables

	private ArrayList<JCheckBox> listaCheckBoxs = new ArrayList<JCheckBox>();
	private List<IObjetoSistema> acessorios;

}